<?php
namespace app\restaurantadmin\controller;
use think\Controller;
use app\restaurantadmin\model\Login as Log;
use think\Captcha;
class Login extends Controller
{
    public function login()
    {

        if(request()->isPost()){//如果是post方式提交过来，就执行登录操作
            $captcha=input('request.captcha');
            if(!captcha_check($captcha)){
                // echo "&nbsp;&nbsp;&nbsp;验证码错误";
                echo("<script type='text/javascript'> alert('验证码错误！');</script>");
            }else{
                $login=new Log;//实例化model类
                $status=$login->login(input('adminname'),input('adminpass'));//login指向login方法，login方法传递两个值
                if($status==1){
                    //在dining表中查询该管理员管理的店铺信息
                    $uid=session('id');
                    $sta=session('sta');
                    $shop= \think\Db::name('dining') ->where('admin_id',$uid)->find();
                    $shopid=$shop['shop_id'];
                    $state=$shop['state'];
                    if(($shopid) && ($state=='1')&&($sta=='0')){
                       return $this->redirect('Index/index');
                    }
                    else if($shopid && $state=='0'){
                        $this->redirect('Index/validate_shop');
                    }
                    elseif (($shopid)&&($state=='1')&&($sta=='1')) {
                        echo("<script type='text/javascript'> alert('账号已被禁用，请联系管理员!');</script>");
                    }
                    elseif (($shopid)&&($state=='2')&&($sta=='0')) {
                      $this->redirect('Index/index');
                    }
                    else{
                        $this->redirect('Index/addshop');
                    }
                }
                elseif($status==2){
                    // echo "&nbsp;&nbsp;&nbsp;账号或者密码错误";
                    echo("<script type='text/javascript'> alert('账号或者密码错误！');</script>");
                    // $this->assign('info',"账号或者密码错误");
                }
                else{
                    // echo "&nbsp;&nbsp;&nbsp;用户不存在";
                    echo("<script type='text/javascript'> alert('用户不存在！');</script>");
                }
            }


        }
        return $this->fetch();
        //echo $data['captcha'];die;
    }


    public function logout(){
        session(null);//清空session
        return $this->redirect('Login/login');
    }
